package TenoriOn
{
	import TenoriOn.UI.TextDisplay;
	import TenoriOn.managers.IGridManager;
	
	import flash.display.Sprite;
	import flash.events.TouchEvent;
	
	public class Grid extends Sprite
	{
		public function Grid()
		{
			graphics.beginFill(0x222222);
			graphics.drawRoundRect(0,0,640,640,40,40);
			lights=new Vector.<Vector.<Light>>(16);
			for (var i:* in lights){
				lights[i]=new Vector.<Light>(16);
				for (var j:* in lights[i]){
					lights[i][j]=new Light(i,j);
					lights[i][j].x=40*i+20;
					lights[i][j].y=40*(15-j)+20;
					lights[i][j].addEventListener(TouchEvent.TOUCH_ROLL_OVER,lightToggle);
					addChild(lights[i][j]);
				}
			}
		}
		
		public function redrawGrid(gridData:GridData):void{
			for (var i:* in lights){
				for (var j:* in lights[i]){
					lights[i][j].state=gridData.data[i][j]
				}
			}
		}
		
		public function lightToggle(event:TouchEvent):void{
			if(_manager)
				_manager.onGridUpdate(event.target.id);
		}
		
		public function get manager():IGridManager{
			return _manager;
		}
		public function set manager(value:IGridManager):void{
			if(_manager)
				_manager.releaseControl();
			_manager=value;
		}
		
		public var textDisplay:TextDisplay;
		
		public var lights:Vector.<Vector.<Light>>;
		
		private var _manager:IGridManager;
	}
}